package com.xiaoxu.bbs.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.bbs.DAO.ReplyDAO;
import com.xiaoxu.bbs.entity.Reply;
import com.xiaoxu.bbs.service.ReplyService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReplyServiceImpl extends ServiceImpl<ReplyDAO, Reply> implements ReplyService {
    @Override
    public Boolean deleteReplyWithItReplys(Long replyId,Long topicId) {
        LambdaQueryWrapper<Reply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(topicId!=null,Reply::getTopicId,topicId);
        List<Reply> replyList = this.list(wrapper);
        List<Long> removeIds = new ArrayList<>();
        for (Reply reply: replyList) {
            Long rereplyId = reply.getRereplyId();
            if(rereplyId.compareTo(replyId) == 0){
                removeIds.add(reply.getId());
            }
        }
        if (removeIds.size() != 0) {
            for (Long id: removeIds) {
                deleteReplyWithItReplys(id,topicId);
            }
            this.removeByIds(removeIds);
        }
        return this.removeById(replyId);
    }
}
